//
//  CommFunc.m
//  SendGzipData
//
//  Created by URANUS on 15/4/14.
//  Copyright (c) 2015年 URANUS. All rights reserved.
//

#import "CommFunc.h"

@implementation CommFunc

static NSMutableDictionary *dictionary = nil;
static NSString *plistPath = nil;
static NSString *salerID = nil;
static NSString *hxPassword = nil;
static NSString *hxUserName = nil;
static NSString *proId = nil;
static NSUserDefaults *userDefaults = nil;

#pragma mark 获取软件UserDefaults 变量
+ (NSUserDefaults *)getUserDefaults{
    if (userDefaults == nil) {
        userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return userDefaults;
}
#pragma mark 获取销售人员ID
+ (NSString *)getSalerUserID{
    NSUserDefaults *userDefaults = [self getUserDefaults];
    salerID = [userDefaults objectForKey:salerID];
    return salerID;
}

#pragma mark 获取环信密码
+ (NSString *)getHxPassword{
    NSUserDefaults *userDefaults = [self getUserDefaults];
    hxPassword = [userDefaults objectForKey:@""];
    return hxPassword;
}

#pragma mark 获取项目名称
+ (NSString *)getProId{
    NSUserDefaults *userDefaults = [self getUserDefaults];
    proId = [userDefaults objectForKey:@""];
    return proId;
}

#pragma mark 获取payId
+ (NSString *)getHxUserName{
    NSUserDefaults *userDefaults = [self getUserDefaults];
    hxUserName = [userDefaults objectForKey:@""];
    return hxUserName;
}

#pragma mark 添加图片网络绝对路径
+ (NSString *)getImageURL:(NSString *)picPath{
    NSString *imageURL = [@"" stringByAppendingPathComponent:picPath];
    return imageURL;
}

#pragma mark 获取单个页面在Config.plist中得配置
+ (NSDictionary *) getPageDirctionary:(NSString *)pageName{
    @synchronized(self){
        if (dictionary == nil) {
            NSString *plistPath = [self getConfigPlistPath];
            dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        }
    }
    
    NSDictionary *pageDirctionary = [dictionary objectForKey:pageName];
    
    return pageDirctionary;
}

#pragma mark 获取Config.plist的root字典
+ (NSString *)getConfigPlistPath{
    if (plistPath == nil) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
    }
    
    return plistPath;
}

+ (void) updateConfigPlistwithKey:(NSString *)key andValue:(id)value{
    NSString *filePath = [self getConfigPlistPath];
    NSMutableDictionary *configDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    [configDictionary setValue:value forKey:key];
    dictionary = configDictionary;
    [configDictionary writeToFile:filePath atomically:YES];
}

#pragma mark获取字典中浮点数
+ (CGFloat) getDictionary:(NSDictionary *)dictionary FloatValue:(NSString *)keyName{
    NSNumber *num = [dictionary objectForKey:keyName];
    CGFloat value = [num floatValue];
    return value;
}

#pragma mark 网络字json到String
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        DDLogInfo(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

#pragma mark 数组到json
+ (NSArray *)arryWithJsonString:(NSString *)jsonString{
    NSError *error;
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    return dataArray;
}

#pragma mark 字典到json
+ (NSString*)dictionaryToJson:(NSDictionary *)dic{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

#pragma mark 发送验证短信的初始函数
//+ (void)SMS_SDKInit{
//    [@"" registerApp:SMSAPPKEY withSecret:SMSAPPSECRET];
//    [@"" enableAppContactFriends:NO];
//}

#pragma 得到frame大小的百分比
+ (CommPos) getPos:(NSString *)infoString{
    NSArray *posArray = [infoString componentsSeparatedByString:@" "];
    CommPos commPos;
    CGFloat x = [[posArray objectAtIndex:0] floatValue];
    CGFloat y = [[posArray objectAtIndex:1] floatValue];
    CGFloat w = [[posArray objectAtIndex:2] floatValue];
    CGFloat h = [[posArray objectAtIndex:3] floatValue];
    commPos.x = x;
    commPos.y = y;
    commPos.w = w;
    commPos.h = h;
    
    return commPos;
}

#pragma mark 通过传入string型RGB值@“255 255 255”和alpha值返回UIColor
+(UIColor *) inPutRGB:(NSString *)rgb inPutAlpha:(double)alpha{
    CommRGB theRGB = [CommFunc getRGB:rgb];
    UIColor *color = [UIColor colorWithRed:theRGB.r green:theRGB.g blue:theRGB.b alpha:alpha];
    return color;
}

#pragma 得到view的RGB色@“255 255 255”
+ (CommRGB) getRGB:(NSString *)rgbString{
    NSArray *rgbArray = [rgbString componentsSeparatedByString:@" "];
    CommRGB commRGB;
    NSInteger r = [[rgbArray objectAtIndex:0] integerValue];
    NSInteger g = [[rgbArray objectAtIndex:1] integerValue];
    NSInteger b = [[rgbArray objectAtIndex:2] integerValue];
    commRGB.r = r / 255.0;
    commRGB.g = g / 255.0;
    commRGB.b = b / 255.0;
    
    return commRGB;
}

#pragma mark 输入字字典名称获取数据函数
+ (NSString *)inPutRGB:(NSString *)rootDirctionaryName dataDirctionary:(NSString *)dirctionary{
    NSDictionary *pageDict = [CommFunc getPageDirctionary:rootDirctionaryName];
    NSString *backgroundColor = [pageDict objectForKey:dirctionary];
    return backgroundColor;
}



@end
